package com.example.content.viewmodule.personal

import androidx.lifecycle.MutableLiveData
import com.example.content.DO_ACTION
import com.example.content.FOLLOW_UN
import com.example.content.UN_ACTION
import com.example.content.repository.RepositoryMgr
import com.example.content.repository.bean.*
import com.example.content.viewmodule.BaseViewModel
import com.example.content.viewmodule.login.TokenStore
import com.example.content.viewmodule.viewdata.CommonViewState
import com.example.core.livedata.SingleLiveEvent

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/28
 */
class PersonalViewModel : BaseViewModel<CommonViewState>() {

    val followersData = MutableLiveData<List<UserInfo>>()
    val productResult = MutableLiveData<List<ProductInfo>>()
    val followResult = MutableLiveData<FollowResp>()

    fun getUserInfo(userId:Int) {
        launch(
            block = {
                if (userId == -1) return@launch
                val req = UserInfoReq(id_list = listOf(userId))
                val apiData = RepositoryMgr.getUserInfo(req)
                if (apiData.code == 200) {
                    followersData.postValue(apiData.apiData())
                }
            }
        )
    }

    //获取用户作品信息
    fun getProductListInfo(id:Int) {
        launch(
            block = {
                productResult.postValue(RepositoryMgr.getUserProducts(id, 1).apiData())
            }
        )
    }

    fun doFollow(userId:Int, relate:Int) {
        launch(
            block = {
                val followReq: FollowReq = if (relate == FOLLOW_UN) {
                    FollowReq(userId, DO_ACTION)
                } else {
                    FollowReq(userId, UN_ACTION)
                }
                followResult.postValue(RepositoryMgr.follow(followReq).apiData())
            }
        )
    }
}